package com.timeDeal.service;


import com.timeDeal.bean.JinduBean;
import com.timeDeal.bean.PageList;
import com.timeDeal.util.DButil;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by 叼爸爸 on 2016/9/6.
 */
public class JInduServicce {
    public  boolean tijiaoJindu(JinduBean jb , String leixing){
        Connection cn = DButil.getConnection();
        String mysql="insert into  jindu   (jinduid, xuqiufabuid,tijiaorenid,miaoshu,zhanshi,banben,qingqiujieshu )"
              +" values(?,?,?,?,?,?,?)";
        PreparedStatement ps=null;
        ResultSet rs=null;
        try{
            ps=cn.prepareStatement(mysql);
            ps.setInt(1,DButil.getZhuJianMax("jinduid","jindu"));
            ps.setInt(2,jb.getXuqiufabuid());
            ps.setInt(3,jb.getTijiaorenid());
            ps.setString(4,jb.getMiaoshu());
            ps.setString(5,jb.getZhanshi());
            ps.setString(6,jb.getBanben());
           ps.setString(7,jb.getShenqingtiqianjieshu());
                  ps.execute();
            return true ;
            }
          catch (SQLException e) {
            e.printStackTrace();
        } finally {
            try {
                if(ps!=null){
                    ps.close();
                }
                if(rs!=null){
                    rs.close();
                }
                cn.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        return  false ;
    }
    public   int  getCountByXuqiuId(int id){
        Connection cn = DButil.getConnection();
        String mysql="select count( *)   from  jindu   where xuqiufabuid=?  "   ;
        PreparedStatement ps=null;
        ResultSet rs=null;
        try{
            ps=cn.prepareStatement(mysql);
            ps.setInt(1,id);
            rs = ps.executeQuery();
            if (rs.next()){
            }
            return  rs.getInt(1);
        }
        catch (SQLException e) {
            e.printStackTrace();
        } finally {
            try {
                if(ps!=null){
                    ps.close();
                }
                if(rs!=null){
                    rs.close();
                }
                cn.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        return   0 ;
    }
    public PageList getJInduByid(int id,int cu){
        Connection connection = DButil.getConnection();
        UserService userService = new UserService() ;
        String mysql="select count( *)   from  jindu   where xuqiufabuid=?  "   ;
        int pagesize = PageList.pagesize ;
        String mysql2="select *   from  jindu   where  xuqiufabuid=?  ORDER by  banben DESC   limit   "+ (cu*pagesize-pagesize)+", "+pagesize   ;
        PageList  pageList =  new PageList() ;
        List<Object> list =new ArrayList<>() ;
        PreparedStatement ps=null;
        ResultSet rs=null;
        try{
            ps=connection.prepareStatement(mysql);
            ps.setInt(1,id);
            rs = ps.executeQuery();
            if (rs.next()){
            }
            pageList.setTotalpage(rs.getInt(1)%pagesize==0?rs.getInt(1)/pagesize:rs.getInt(1)/pagesize+1) ;
            pageList.setCurrentpage(cu);
            ps.clearParameters();
            ps=connection.prepareStatement(mysql2);
            ps.setInt(1,id);
            rs=ps.executeQuery() ;
            while (rs.next()){
                JinduBean jinduBean = new JinduBean() ;
                jinduBean.setJinduid(rs.getInt("jinduid"));
                jinduBean.setXuqiufabuid(rs.getInt("xuqiufabuid"));
                jinduBean.setTijiaorenid(rs.getInt("tijiaorenid"));
                jinduBean.setBanben(rs.getString("banben"));
                jinduBean.setMiaoshu(rs.getString("miaoshu"));
                jinduBean.setZhanshi(rs.getString("zhanshi"));
                jinduBean.setShenqingtiqianjieshu(rs.getString("qingqiujieshu"));
                list.add(jinduBean);
            }
            pageList.setList(list);
            return  pageList ;
        }
        catch (SQLException e) {
            e.printStackTrace();
        } finally {
            try {
                if(ps!=null){
                    ps.close();
                }
                if(rs!=null){
                    rs.close();
                }
                connection.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        return  null ;
    }
}
